package com.ytclub.subject.infra.rpc;

import com.ytclub.auth.api.UserFeignService;
import com.ytclub.auth.entity.AuthUserDTO;
import com.ytclub.auth.entity.Result;
import com.ytclub.subject.infra.entity.UserInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * auth微服务调用类
 *
 * @author: HuangQi
 * @date: Created in 11:01 2024/4/18
 * @description: auth微服务调用
 */
@Component
public class UserRPC {

    @Resource
    private UserFeignService userFeignService;

    public UserInfo getUserInfo(String userName) {
        AuthUserDTO authUserDTO = new AuthUserDTO();
        authUserDTO.setUserName(userName);

        // 调用远程服务
        Result<AuthUserDTO> result = userFeignService.getUserInfo(authUserDTO);

        // 封装返回结果
        UserInfo userInfo = new UserInfo();

        // 如果调用失败，返回空对象
        if (!result.getSuccess()) {
            return userInfo;
        }

        // 如果调用成功，封装返回结果
        AuthUserDTO data = result.getData();
        userInfo.setUserName(data.getUserName());
        userInfo.setNickName(data.getNickName());
        userInfo.setAvatar(data.getAvatar());
        return userInfo;
    }


}
